<?php
/**
 * 地区服务层
 * @author maniac
 */
namespace app\common\service;
use Symfony\Component\Yaml\Tests\B;

class Regions extends Base
{
    private $jsonFile  = '';        // json 文件路径
    private $addr_data = [];        // 区域数据
    private $region_data=[];
    
    public function __construct()
    {
        $this->jsonFile = './static/data/address.json';
    }
    
    /**
     * 获取区域
     * @param  integer $provinceId 省份 id
     * @param  integer $cityId     城市 id
     * @param  integer $districtId 区域 id
     * @return array
     */
    public function regions($provinceId = 0, $cityId = 0, $districtId = 0)
    {

        if (empty($this->addr_data)) {
            $this->initData();
        }
        if (!$provinceId) {
            foreach ($this->addr_data as $key => $val) {
                $data[] = [
                    'id'   => $val['id'],
                    'name' => $val['name'],
                ];
            }
            return $data;
        }
        $data = $this->addr_data[$provinceId]['child'];
        if (!$cityId) {
            foreach ($data as $key => $val) {
                $data[$key] = [
                    'id'   => $val['id'],
                    'name' => $val['name'],
                ];
            }
            return array_values($data);
        }
        $data = $data[$cityId]['child'];
        if ($districtId) {
            $data = $data[$districtId];
        } else {
            $data = array_values($data);
        }
        return $data;
    }
    
    /**
     * 获取地区信息
     * @return array
     */
    public function getAddrData()
    {
        if (empty($this->addr_data)) {
            $this->initData();
        }
        return $this->addr_data;
    }

    /**
     * 按地区名称获取ID
     * @param $region_name
     * @return int|string
     */
    public function getRegionId($region_name){
        if (empty($this->addr_data)) {
            $this->initData();
        }
        $region_id=0;
        foreach ($this->region_data as $k=>$v){
            if($region_name==$v){
                $region_id=$k;
                break;
            }
        }
        return $region_id;
    }

    //获取城市列表
    public function getRegionList(){
        if (empty($this->addr_data)) {
            $this->initData();
        }
        return $this->region_data;
    }
    
    /**
     * init
     */
    private function initData()
    {
        $json = file_get_contents($this->jsonFile);
        $arr  = json_decode($json, true);
        $data = [];
        $tmp_region_data=[];
        foreach ($arr as $v) {
            $tmp_region_data[$v["region_id"]]=$v["region_name"];
            $temp2 = [];
            foreach ($v['child'] as $vv) {
                $tmp_region_data[$vv["region_id"]]=$vv["region_name"];
                $temp3 = [];
                foreach ($vv['child'] as $vvv) {
                    $tmp_region_data[$vvv["region_id"]]=$vvv["region_name"];
                    $temp3[$vvv['region_id']] = [
                        'id' => $vvv['region_id'],
                        'parent_id' => $vvv['parent_id'],
                        'name' => $vvv['region_name'],
                    ];
                }
                $temp2[$vv['region_id']] = [
                    'id' => $vv['region_id'],
                    'parent_id' => $vv['parent_id'],
                    'name' => $vv['region_name'],
                    'child' => $temp3,
                ];
            }
            $data[$v['region_id']] = [
                'id' => $v['region_id'],
                'parent_id' => $v['parent_id'],
                'name' => $v['region_name'],
                'child' => $temp2,
            ];
        }

        $this->region_data=$tmp_region_data;
        $this->addr_data = $data;
    }


}
